﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WindowsFormsApplication2;

namespace Anata_Client.Model
{
    class MetodePembayaran
    {
        public int ID_Transaksi;
        DateTime TanggalMasuk;
        DateTime TanggalKeluar;
        int ID_Konsumen;
        public int TotalHarga;
        int Nominal;
        int Persentase;
        int Correction;
        
        public MetodePembayaran()
        {
            
        }

        public MetodePembayaran(int _ID_Transaksi)
        {
            Database db = new Database();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            string sql = "SELECT * FROM transaksi WHERE ID_Transaksi = '" + _ID_Transaksi + "'";

            db.Open();
            ds = db.ExecuteQuery(sql);
            dt = ds.Tables[0];
            DataRow dr = dt.Rows[0];

            ID_Transaksi = (int)dr[0];
            TanggalMasuk = (DateTime)dr[1];
            TotalHarga = (int)dr[4];
            
            db.Close();
        }

        public static ArrayList DaftarMetode()
        {
            ArrayList a = new ArrayList();
            Database db = new Database();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            object[] o = new object[2];
            string sql = "SELECT * FROM metode_pembayaran";

            db.Open();
            ds = db.ExecuteQuery(sql);
            dt = ds.Tables[0];
            DataRow dr;

            for (int i = 0; i < dt.Rows.Count; ++i)
            {
                dr = dt.Rows[i];

                o[0] = (int)dr[0];
                o[1] = (string)dr[1];
                a.Add(o.Clone());
            }

            db.Close();

            return a;
        }

        public void AddTransaksi(string _TanggalMasuk, int _ID_Konsumen)
        {
            Database db = new Database();
            long l;
            string sql = "INSERT INTO transaksi(\"TanggalMasuk\", \"ID_Konsumen\") VALUES ('" + _TanggalMasuk + "'," + _ID_Konsumen.ToString() + ");select currval('\"Transaksi_ID_Transaksi_seq\"')";

            db.Open();
            l = db.ExecuteNonQuery(sql);
            db.Close();

            ID_Transaksi = (int)l;

            System.Console.WriteLine(l.ToString());
        }

        public void AddTransaksi(string _TanggalMasuk, int _ID_Konsumen, int _totalHarga)
        {
            Database db = new Database();
            long l;
            string sql = "INSERT INTO transaksi(TanggalMasuk, ID_Konsumen, TotalHarga) VALUES ('" + _TanggalMasuk + "'," + _ID_Konsumen.ToString() + "," + _totalHarga.ToString() + ");select currval('Transaksi_ID_Transaksi_seq')";

            db.Open();
            l = db.ExecuteNonQuery(sql);
            db.Close();

            System.Console.WriteLine(l.ToString());
        }

        public void UpdateTransaksi(int _idTransaksi, int _harga)
        {
            Database db = new Database();
            string sql = "UPDATE transaksi SET \"TotalHarga\" = " + _harga.ToString() + " WHERE \"ID_Transaksi\" = " + _idTransaksi.ToString();

            db.Open();
            db.ExecuteDelete(sql);
            db.Close();
        }

        public void DeleteTransaksi(int _idTransaksi)
        {
            Database db = new Database();
            string sql = "DELETE FROM transaksi WHERE \"ID_Transaksi\" = " + _idTransaksi.ToString();

            db.Open();
            db.ExecuteDelete(sql);
            db.Close();
        }
    }
}
